<template>
  <div class="mod-config">
    <table-view ref="view"
                :searchAttrs="searchAttrs"
                :primaryKey="primaryKey"
                :listApiName="listApiName"
                :deleteApiName="deleteApiName"
                :exportApiName="exportApiName"
                :saveAuthName="saveAuthName"
                :deleteAuthName="deleteAuthName"
                :showAttrs="showAttrs"
                :printFunc='printFunc'
                :multiAdd='true'
                :sumInfo='sumInfo'
    >
      <template v-slot:header>
<!--        <el-button v-if="sAuth(downloadHandleName)" size="small" type="primary" @click="downloadHandle">对账单下载-->
<!--        </el-button>-->
      </template>
    </table-view>
    <add-or-update v-if="addOrUpdateVisible" ref="addOrUpdate" @refreshDataList="getDataList"></add-or-update>
    <client-info-add-or-update v-if="clientInfoVisible" ref="clientInfo"></client-info-add-or-update>
  </div>
</template>

<script>
import AddOrUpdate from './productleavestorage-add-or-update'
import tableView from '@/components/tableView'
import basicMixins from '@/utils/commonViewMixins'
import ClientInfoAddOrUpdate from '@/views/modules/client/clientinfo-add-or-update.vue'
import {exportHandle, isAuth} from '@/utils'
import {API_DEFAULT_CONFIG} from '@/config'

export default {
  mixins: [basicMixins],
  data() {
    return {
      sAuth: isAuth,
      clientInfoVisible: false,
      searchAttrs: {
        clientContractNo:{
          placeholder: '业务合同编号'
        },
        productNo: {
          placeholder: '请选择产品'
        },
        clientId: {
          type: 'autoComplete',
          placeholder: '业务员',
          querySearchApiName: 'clientApi/getClientList'
        },
        dateTimeRange: {
          type: 'date',
          dateType: 'daterange',
          clearable: true
        }
      },
      primaryKey: 'id',
      listApiName: 'productApi/listProductLeaveStorage',
      deleteApiName: 'productApi/deleteProductLeaveStorage',
      downloadApiName: 'productApi/getProductReconciliationExport',
      saveAuthName: 'product:productleavestorage:save',
      deleteAuthName: 'product:productleavestorage:delete',
      downloadHandleName: 'finance:financepay:list',
      exportApiName: 'productApi/getProductLeaveStorageExport',
      sumInfo:{realSaleNumber: 'realSaleNumber',purchaseCost:'purchaseCost'},
      showAttrs: {
        clientContractNo: {label: '业务合同编号',width: 200},

        sampleImage:{
          label: '产品图片',
          type: 'image'
        },
        productNo: {
          label: '产品编号',
          type: 'linkLabel',
          routerName: 'product-product-info-all',
          searchKey: 'productNo',
        },
        customerRequire:{
          label: "规格描述",
        },
        realSaleNumber: {label: '销售数量'},
        purchaseCost: {label: '采购成本'},
        groupType:{
          label: '配套组合'
        },
        saleName: {
          label: '业务员'
        },
        outTime: {label: '出库时间', type: 'date',width: 100},
        productWeight: {label: '克数', width:80},
        // orderDetailNo: {
        //   label: '订单编号',
        //   width:180
        // },

        tickerNo: {label: '出库单号',width: 150},
        // boxSupplyWay: {
        //   label: '包装规格',
        //   type: 'radio',
        //   items: [
        //     {value: 0, text: '纸箱', tagType: 'success'},
        //     {value: 1, text: '托盘'},
        //     {value: 2, text: '客供', tagType: 'danger'}
        //   ]
        // },
      //  boxNo: {label: '纸箱编号'},
      //   boxZhiNumber: {label: '箱只'},
      //   boxNumber: {label: '箱数'},
       // trayInfoNo: {label: '托盘编号'},
       // trayNumber: {label: '托盘数量'},


        remark: {label: '备注'},
        status: {
          label: '状态',
          type: 'radio',
          items: [
            {value: -1, text: '缺货',tagType: 'danger'},
            {value: 0, text: '待配货', tagType: 'info'},
            {value: 1, text: '正常',}
          ]
        }
      }
    }
  },
  computed: {
    imageUrl() {
      return `${API_DEFAULT_CONFIG.baseURL}/pub`
    },
    token() {
      return this.$cookie.get('token')
    }
  },
  methods: {
    printFunc(rows) {
      this.$vBus.$emit('SHOW_PRINT_DIALOG', (win) => {
        const tmpDocument = this.R.prop('document', win)
        const content = tmpDocument.querySelector('#content')

        if (this.R.isNil(tmpDocument) || this.R.isNil(content)) {
          return
        }
        content.innerHTML = ''

        const addTd = (tr, str) => {
          const td = tmpDocument.createElement('td')
          if (str && str.length > 0) {
            if (str.indexOf('images') != -1) {
              td.innerHTML = '<span><img src='+this.imageUrl+str+'?token='+this.token+' alt="" style="opacity: .8; height:100px; width:100px;"></span>'
            } else {
              td.innerText = str
            }
          } else {
            td.innerText = str
          }

          tr.appendChild(td)
        }
        const {outTime} = rows[0]
        const {tickerNo} = rows[0]
        const {saleName} = rows[0]

        tmpDocument.querySelector('#time').innerHTML = outTime.toString().substr(0,11)
        tmpDocument.querySelector('#tickerNo').innerHTML = tickerNo.toString()
        tmpDocument.querySelector('#saleName').innerHTML = saleName ? saleName : ''

        rows.forEach(row => {
          const {sampleImage, productNo, productWeight, boxSupplyWay, customerRequire, groupType, trayLayerNumber, boxNumber, trayNumber, weightCount,tonnageWeightCount, realSaleNumber,remark} = row
         // const boxSupplyWayName = boxSupplyWay === 0 ? '纸箱' : boxSupplyWay===1 ? '托盘': '自供'
          const tr = tmpDocument.createElement('tr')

          addTd(tr, sampleImage)
          addTd(tr, productNo)
        //  addTd(tr, customerRequire ? customerRequire : '')
          addTd(tr, '测试数据')
        //  addTd(tr, groupType? groupType : '')
          addTd(tr, '测试组合')
          addTd(tr, realSaleNumber)

          // addTd(tr, boxSupplyWayName)
          // if (boxSupplyWay === 0) {
          //   addTd(tr, boxZhiNumber)
          //   addTd(tr, boxNumber)
          // }
          // else if (boxSupplyWay === 1) {
          //   addTd(tr, trayEachZhiNumber * trayLayerNumber)
          //   addTd(tr, trayNumber)
          // } else {
          //   addTd(tr, boxZhiNumber )
          //   addTd(tr, boxNumber)
          // }
          // addTd(tr, productWeight)
          // addTd(tr, tonnageWeightCount)
          addTd(tr, remark)

          content.appendChild(tr)
        })
      }, true,'print.html')
    },
    downloadHandle() {
      console.log(this.searchAttrs.clientId)
      // if (this.searchAttrs.clientId) {
      //   return exportHandle(this.downloadApiName,{clientId: this.searchAttrs.clientId})
      // }else {
      //   return exportHandle(this.downloadApiName)
      // }

    },
    addMulti() {
      this.$refs.editTableView.open('批量出库')
    },
    openClientInfoDialog(cb) {
      this.clientInfoVisible = true
      this.$nextTick(() => {
        if (!this.R.isNil(this.$refs.clientInfo)) {
          this.$refs.clientInfo.init(null, {status: 1}, cb)
        }
      })
    }
  },
  components: {
    tableView,
    AddOrUpdate,
    ClientInfoAddOrUpdate
  }
}
</script>
